package org.wevil.anno;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.wevil.util.StringUtil;

import java.io.IOException;
import java.util.function.Function;

/**
 * 自定义json序列化
 */
public final class DataMaskingSerializer extends JsonSerializer<String> {

    Function<String, String> operation;

    public DataMaskingSerializer(Function<String, String> operation) {
        super();
        // 指定当前序列化用哪个方法
        this.operation = operation;
    }

    @Override
    public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(StringUtil.isBlank(value)){
            jsonGenerator.writeString("这是个空值");
        }
        else{
            jsonGenerator.writeString(operation.apply(value));
        }
    }
}
